package gao.xiaolei.controller;

import gao.xiaolei.dto.*;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*;
import gao.xiaolei.exception.AuthException;
import gao.xiaolei.service.ClassesService;
import gao.xiaolei.service.UserService;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/classes")
public class ClassesController {

    @Resource(name = "ClassesService")
    private ClassesService classesService;

    @Resource(name = "UserService")
    private UserService userService;

    @PostMapping()
    public void addClasses(@RequestBody ClassesInDto classesInDto) {
        classesService.addClasses(classesInDto, false);
    }

    @PutMapping()
    public void updateClasses(@RequestBody ClassesInDto classesInDto) {
        classesService.updateClasses(classesInDto);
    }

    @DeleteMapping("/{id}/{currentUserId}")
    public void deleteClasses(@PathVariable("id") String id,
                              @PathVariable("currentUserId") String currentUserId) {
        classesService.deleteClasses(id, currentUserId);
    }

    @GetMapping("/leader/{leader}")
    public List<TreeDto> findClassesListByLeader(@PathVariable("leader") String leader) {
        return classesService.getRootClassesByLeader(leader);
    }

    @GetMapping("/creater/{creater}")
    public List<TreeDto> findClassesListByCreater(@PathVariable("creater") String creater) {
        return classesService.getRootClassesByCreater(creater);
    }

    @GetMapping("/detail/{id}")
    public ClassesDto getClassesDetail(@PathVariable("id") String id) {
        return classesService.getClassesDetail(id);
    }

    @GetMapping("/user/inWitchGroup/{classesId}/{userId}")
    public List<String> getUserInClassesInWitchGroup(@PathVariable("classesId") String classesId,
                                                     @PathVariable("userId") String userId) {
        return classesService.getUserInClassesInWitchGroup(classesId, userId);
    }

    //获取班级的成员列表
    @GetMapping("/members/{classesId}")
    public List<ClassesMemberDto> getClassesMember(@PathVariable("classesId") String classesId) {
        return classesService.getClassesMember(classesId);
    }

    @GetMapping("/allDirector")
    //获取所有主任列表（不是指某个班级）
    public List<UserOutDto> getAllDirectorList() {
        return userService.getDirectorList();
    }

    @GetMapping("/allTeacher")
    //获取所有老师列表（不是指某个班级）
    public List<UserOutDto> getAllTeacherList() {
        return userService.getTeacherList();
    }

    @GetMapping("/allStudent")
    //获取所有学生列表（不是指某个班级）
    public List<UserOutDto> getAllStudentList() {
        return userService.getStudentListList();
    }

    @GetMapping("/forAdmin/list/{currentUserId}")
    //管理员查看班级列表
    public List<ClassesListDto> getClassesListForAdmin(@PathVariable("currentUserId") String currentUserId) {
        return classesService.getClassesListForAdmin(currentUserId);
    }

    @GetMapping("/forAdmin/detail/{id}")
    //管理员查看班级详情
    public ClassesAndProcessOutDto getClassesDetailForAdmin(@PathVariable("id") String id) {
        return classesService.getClassesDetailForAdmin(id);
    }

    @PostMapping("/forAdmin")
    //管理员添加班级
    public void addClassesForAdmin(@RequestBody ClassesAndProcessInDto classesAndProcessInDto) {
        classesService.addClassesForAdmin(classesAndProcessInDto);
    }

    @PutMapping("/forAdmin")
    //管理员修改班级
    public void updateClassesForAdmin(@RequestBody ClassesAndProcessInDto classesAndProcessInDto) {
        classesService.updateClassesForAdmin(classesAndProcessInDto);
    }

    @PutMapping("/forAdmin/del/{currentUserId}")
    //管理员删除班级
    public void deleteClassesForAdmin(@RequestBody List<String> classesIds,
                                      @PathVariable("currentUserId") String currentUserId) {
        classesService.deleteClassesForAdmin(classesIds, currentUserId);
    }

    @PutMapping("/forAdmin/manual/Process")
    //管理员直接手动完成班级流程，即不按时间规划，直接完成开班
    public void manualOverClassesProcess(@RequestParam("classesId") String classesId,
                                         @RequestParam("currentUserId") String currentUserId) {
        classesService.manualOverClassesProcess(classesId, currentUserId);
    }

    @GetMapping("/onTime/openClasses/{classesId}")//因为是内部请求就直接用get操作了
    //班级到了开班截止时间要执行的事情
    public void classesOpenOnTimeWork(@PathVariable("classesId") String classesId) {
        classesService.classesOnTimeWork(classesId);
    }

    @GetMapping("/name/group/{groupId}")
    //通过组Id获取班级名字
    public String getClassesNameByGroupId(@PathVariable("groupId") String groupId) {
        return classesService.getClassNameByGroupId(groupId);
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseEnvelope<String>> handelError(Exception e) {
        e.printStackTrace();
        Integer code = null;
        if (e.getClass() == MissingServletRequestParameterException.class)
            code = 400;
        else if (e.getClass() == AuthException.class)
            code = 401;
        else
            code = 500;
        ResponseEnvelope<String> result = new ResponseEnvelope<String>(code, e.getMessage(), null);
        return new ResponseEntity<ResponseEnvelope<String>>(result, HttpStatus.valueOf(500));
    }

}
